package es.agroservicio.Agroservicio.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import es.agroservicio.Agroservicio.domain.Client;
import es.agroservicio.Agroservicio.repository.ClientsDAO;
import es.agroservicio.Agroservicio.service.ClientsManager;

@Controller
public class ClientsController{

	@Autowired
	private ClientsManager clientManager;

	@Autowired
	private ClientsDAO clientsDao;

	protected final Log logger = LogFactory.getLog(getClass());

	// @RequestMapping(method = RequestMethod.GET)
	// public String displaySortedClients(Model model) {
	// // model.addAttribute("newClient", new Client());
	//
	// if (clientsDao != null)
	// if (clientsDao.findAllOrderedByName() != null)
	// model.addAttribute("clients", clientsDao.findAllOrderedByName());
	// return "index";
	// }

//	@RequestMapping(value = "/addUpdateClient.html", method = RequestMethod.POST)
//	public String save(@Valid @ModelAttribute("newClient") Client newClient,
//			BindingResult result, Model model) {
//		if (!result.hasErrors()) {
//			clientsDao.saveOrUpdate(newClient);
//			return "redirect:/hello.html";
//		} else {
//			model.addAttribute("clients", clientsDao.findAllOrderedByName());
//			return "redirect:/hello.html";
//		}
//	}



	// public ModelAndView handleRequest(HttpServletRequest request,
	// HttpServletResponse response) throws ServletException, IOException {
	//
	// return new ModelAndView("index.jsp");
	// }

	@RequestMapping(value = "/clientes.html")
	public @ResponseBody
	ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String now = (new Date()).toString();
		logger.info("Returning hello view with " + now);

		Map<String, Object> myModel = new HashMap<String, Object>();
		myModel.put("now", now);
		myModel.put("arrClients", this.clientManager.getClients());

		// Client cli = this.clientManager.getClients().get(0);
		// myModel.put("cliente", cli);

		List<String> names = new ArrayList<String>();
		names.add("\"Juan Javier\"");
		names.add("\"Antonio\"");
		names.add("\"Ana\"");

		myModel.put("clientsNames", names);

		System.out.println(new ModelAndView("clients", "model", myModel));

		return new ModelAndView("clients", "model", myModel);
	}

	@RequestMapping(value = "/getClient.html", method = RequestMethod.GET)
	public @ResponseBody
	Client getClient(@RequestParam("dni") String dni) throws Exception {

		return this.clientManager.getClientById(dni);
	}

	@RequestMapping(value = "/getClientsIdNames.html", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> getClientsIdNames() throws Exception {

		return this.clientManager.getClientsIdNames();
	}

	@RequestMapping(value = "/pruebas.html", method = RequestMethod.GET)
	public @ResponseBody
	List<Client> getClient() throws Exception {

		return this.clientManager.getClients();
	}

	@RequestMapping(value = "/hello.html")
	public ModelAndView ha(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String now = (new Date()).toString();
		logger.info("Returning hello view with " + now);

		Map<String, Object> myModel = new HashMap<String, Object>();
		myModel.put("now", now);

		return new ModelAndView("hello", "model", myModel);
	}

}
